<?php
Class Controller_Manage_ProCat extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建分类';
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建分类保存';
		$name = isset($_POST['name'])?$_POST['name']:'';
		$notes = isset($_POST['notes'])?$_POST['notes']:'';
		
		if($name)
		{
			$exsit = VegeProCat::find('name=?',$name)->asArray()->getOne();
			if(isset($exsit['id'])&&$exsit['id'])
			{
				return $this->_redirectMessage('提交失败', "对应的分类信息已经存在", url('Manage::ProCat/Create'),FAILURE);
			}
			
			$new = new VegeProCat();
			$new->name = $name;
			$new->notes = $notes;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::ProCat/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::ProCat/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '分类信息列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeProCat::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::ProCat/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '分类信息详情';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$product = VegeProCat::find('id=?', $id)->asArray()->getOne();
		if ($product)
		{
			$this->_view['product'] =  $product;
		}
		else
		{
			return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::ProCat/List'), FAILURE);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '分类编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$pc = VegeProCat::find('id=?', $id)->asArray()->getOne();
		if ($pc)
		{
			$this->_view['pc'] = $pc;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '分类不存在', url('Manage::ProCat/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '分类编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$name = isset($_POST['name'])?$_POST['name']:'';
			$notes = isset($_POST['notes'])?$_POST['notes']:'';
			
			if($name)
			{
				$edit = VegeProCat::find('id=?', $id)->getOne();
				$edit->name = $name;
				$edit->notes = $notes;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::ProCat/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '删除';
		$result = VegeProCat::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegeProCat::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
}